﻿@*------------------------------------------------------------
  Copyright (c) Microsoft Corporation.  All rights reserved.
  Licensed under the MIT License (MIT). See License.txt in the repo root for license information.
------------------------------------------------------------*@

@page "/publishers"

@using Microsoft.AspNetCore.Components;
@using Microsoft.Azure.IIoT.App.Services;
@using Microsoft.Azure.IIoT.App.Components.Buttons;
@using Microsoft.Azure.IIoT.App.Components.Drawer;
@using Microsoft.Azure.IIoT.App.Components.ErrorMessage;
@using Microsoft.Azure.IIoT.App.Components.Loader;
@using Microsoft.Azure.IIoT.OpcUa.Api.Registry;
@using Microsoft.Azure.IIoT.OpcUa.Api.Registry.Models
@using Microsoft.Azure.IIoT.App.Common;
@using Microsoft.Azure.IIoT.App.Components.Icons;
@using Microsoft.Azure.IIoT.App.Models;

@inject NavigationManager NavigationManager
@inject Registry RegistryHelper
@inject IRegistryServiceApi RegistryService
@inject RegistryServiceEvents RegistryServiceEvents
@inject UICommon CommonHelper

@inherits IIoTItemsCollection<PublisherApiModel>

<h1>Publishers</h1>

<table class="table table-responsive @_tableView">
    <thead>
        <tr>
            <th class="width-large">Id</th>
            <th class="width-large">Configuration</th>
            <th class="width-medium">Site Id</th>
            <th class="width-small">Sync Status</th>
            <th class="width-small">Connection Status</th>
            <th class="width-small">Version</th>
        </tr>
    </thead>
    <tbody>
        @foreach (var publisher in Items.Results)
        {
            <tr>
                <td class="hover-text width-large">
                    <div>
                        @publisher.Id
                    </div>
                </td>
                <td class="width-large">
                    <div class="space-wrap">
                        @{ string Capabilities = CommonHelper.None;}
                        @{ string HeartbeatInterval = CommonHelper.None;}
                        @{ string JobCheckInterval = CommonHelper.None;}
                        @{ string JobOrchestratorUrl = CommonHelper.None;}
                        @{ string MaxWorkers = CommonHelper.None;}
                        @if (publisher.Configuration != null)
                        {
                            Capabilities = publisher.Configuration.Capabilities == null ? CommonHelper.None : publisher.Configuration.Capabilities.ToString();
                            HeartbeatInterval =
                                !IsTimeIntervalSet(publisher.Configuration.HeartbeatInterval) ?
                                    CommonHelper.None : publisher.Configuration.HeartbeatInterval.Value.TotalSeconds.ToString() + " sec";
                            JobCheckInterval =
                                !IsTimeIntervalSet(publisher.Configuration.JobCheckInterval) ?
                                    CommonHelper.None : publisher.Configuration.JobCheckInterval.Value.TotalSeconds.ToString() + " sec";
                            JobOrchestratorUrl = string.IsNullOrEmpty(publisher.Configuration.JobOrchestratorUrl) ? CommonHelper.None : publisher.Configuration.JobOrchestratorUrl;
                            MaxWorkers = publisher.Configuration.MaxWorkers == null ? CommonHelper.None : publisher.Configuration.MaxWorkers.ToString();
                        }
                        Capabilities: @Capabilities<br />
                        HeartbeatInterval:
                        <a href="javascript: void(0)" @onclick="@(() => OpenDrawer(publisher))">
                            @HeartbeatInterval
                        </a>
                        <br />
                        JobCheckInterval:
                        <a href="javascript: void(0)" @onclick="@(() => OpenDrawer(publisher))">
                            @JobCheckInterval
                        </a>
                        <br />
                        JobOrchestratorUrl: @JobOrchestratorUrl<br />
                        MaxWorkers:
                        <a href="javascript: void(0)" @onclick="@(() => OpenDrawer(publisher))">
                            @MaxWorkers
                        </a>
                    </div>
                </td>
                <td class="hover-text width-medium">
                    <div>
                        @(string.IsNullOrEmpty(publisher.SiteId)
                            ? CommonHelper.None
                            : publisher.SiteId)
                    </div>
                </td>
                <td class="hover-text width-small">
                    <div>
                        @(publisher.OutOfSync == null
                            ? CommonHelper.None
                            : publisher.OutOfSync == true
                                ? "Out Of Sync"
                                : "Synced")
                    </div>
                </td>
                @{string connectStatus = publisher.Connected == null
                                  ? CommonHelper.None
                                  : publisher.Connected == true
                                      ? "Connected"
                                      : "Disconnected";}
                <td class="hover-text width-small">
                    <ConnectStatus Status="@connectStatus" />
                </td>
                <td class="hover-text width-small">
                    <div>
                        @publisher.Version
                    </div>
                </td>
            </tr>
        }
    </tbody>
</table>
<div class="@_tableEmpty center">No Publishers found.</div>

<Drawer HeaderText="Publisher Config" ObjectData="@Publisher" IsOpen="@IsOpen" CloseDrawer="@(() => CloseDrawer())">
    <_DrawerPublisher Publisher="@Publisher"></_DrawerPublisher>
</Drawer>

<ErrorMessage PageError="@Items.Error" Status="@null"></ErrorMessage>

<LoadMore Result=@Items IsLoading=@IsLoading LoadMoreItems=@(async() => await LoadMoreItems()) />

<Spinner IsLoading=@IsLoading></Spinner>